package com.julis.activity.obu_function;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baseutils.File_Z;
import com.github.czy1121.view.RoundButton;
import com.hb.dialog.dialog.ConfirmDialog;
import com.julis.R;
import com.julis.adapter.OBU_jiance_FragmentAdapter;
import com.julis.bean.FanHui;
import com.julis.bean.MyEvent;
import com.julis.thread.ActivateOfflineThread;
import com.julis.thread.DetectionSubThread;
import com.kaopiz.kprogresshud.KProgressHUD;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 *设备激活
 */
public class OBU_Activate_Activity extends AppCompatActivity {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.check)
    RoundButton check;
    @BindView(R.id.tv_tishi)
    TextView tvTishi;
    @BindView(R.id.activate)
    RoundButton activate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_obu_activate);
        ButterKnife.bind(this);
        tvTitle.setText("OBU激活");

        initview();
    }


    private void initview() {

        pager.setAdapter(new OBU_jiance_FragmentAdapter(getSupportFragmentManager(), OBU_Activate_Activity.this));
        tabs.setTabMode(TabLayout.MODE_FIXED);
        tabs.setupWithViewPager(pager);// 将viewpager和mIndicator关联起来,必须在viewpager设置完adapter后才能调用


    }


    DetectionSubThread detection;
    ActivateOfflineThread activateOfflineThread;

    @OnClick({R.id.img_back, R.id.check, R.id.activate})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.activate:
                 File_Z. GetNowDate2();
                activate();
                break;

            case R.id.check:

                check();
                break;
        }
    }
    public  void check(){

        check.setEnabled(false);
        show();
        File_Z. GetNowDate2();
        detection = new DetectionSubThread(OBU_Activate_Activity.this, mHandler);
        detection.start();
    }
public  void activate(){
    if (OBUMAC == null) {
        Toast.makeText(this, "请先检测OBU！", Toast.LENGTH_SHORT).show();
        return;
    }
    show();
    File_Z. GetNowDate2();
    activateOfflineThread = new ActivateOfflineThread(OBU_Activate_Activity.this, OBUMAC, mHandler);
    activateOfflineThread.start();
}
    KProgressHUD dialog;

    public void show() {
        dialog = KProgressHUD.create(OBU_Activate_Activity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("提示")
                .setDetailsLabel("正在检测OBU，请稍等!")
                .setCancellable(true)
                .setAnimationSpeed(2)
                .setDimAmount(0.5f)
                .show();
    }

    String OBUMAC;
    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {

        //handleMessage为处理消息的方法
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:
                    //不一定出來在哪个线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            FanHui fanhui = (FanHui) msg.obj;
                            OBUMAC = fanhui.getBundle().getString("OBUMAC");

                            tvTishi.setTextColor(getResources().getColor(R.color.black));
                            tvTishi.setText("" + fanhui.getMessage());
                            MyEvent tianQi = new MyEvent();
                            tianQi.setCode("1");
                            tianQi.setFanHui(fanhui);
                            EventBus.getDefault().post(tianQi);
                        }
                    });
                    break;
                case 2:
                    //不一定出來在哪个线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            OBUMAC = null;
                            FanHui fanhui = (FanHui) msg.obj;
                            tvTishi.setTextColor(getResources().getColor(R.color.red));
                            tvTishi.setText("" + fanhui.getMessage());
                            MyEvent tianQi = new MyEvent();
                            tianQi.setCode("2");
                            tianQi.setFanHui(null);
                            EventBus.getDefault().post(tianQi);
                        }
                    });
                    break;
                case 3:
                    //不一定出來在哪个线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            FanHui fanhui = (FanHui) msg.obj;
                            MyEvent tianQi = new MyEvent();
                            tianQi.setCode("3");
                            tianQi.setFanHui(fanhui);
                            EventBus.getDefault().post(tianQi);
                            final ConfirmDialog confirmDialog = new ConfirmDialog(OBU_Activate_Activity.this);
                            confirmDialog.setLogoImg(R.mipmap.dialog_notice).setMsg("激活成功");
                            confirmDialog.setCancelable(false);
                            confirmDialog.setPositiveBtn(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    check();
                                    confirmDialog.dismiss();
                                }
                            });
                            confirmDialog.show();
                        }
                    });
                    break;
                case 4:
                    //不一定出來在哪个线程
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            FanHui fanhui = (FanHui) msg.obj;
                            tvTishi.setTextColor(getResources().getColor(R.color.red));
                            tvTishi.setText("" + fanhui.getMessage());
                            MyEvent tianQi = new MyEvent();
                            tianQi.setCode("4");
                            tianQi.setFanHui(null);
                            EventBus.getDefault().post(tianQi);
                        }
                    });
                    break;

                default:
                    break;
            }
            check.setEnabled(true);
            if (dialog != null) {
                dialog.dismiss();

            }
            super.handleMessage(msg);
        }
    };


}
